﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Code4WP7SDK.Controls
{
    public class Code4CalendarItem : Button
    {
        #region Fields
        private Code4Calendar _owner;
        private Border _OutBorder;
        #endregion

        #region override
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Foreground = AppColorHelper.GetPhoneForeGround();
            this.Background = new SolidColorBrush(Colors.Transparent);
            SetBackColor();
            SetForeColor();
        }
        #endregion
        #region Constructor
        public Code4CalendarItem()
        {
            this.DefaultStyleKey = typeof(Code4CalendarItem);
        }
        public Code4CalendarItem(Code4Calendar owner)
        {
            this.DefaultStyleKey = typeof(Code4CalendarItem);
            _owner = owner;
        }
        #endregion

        #region Dependency Properties

        public int DayNumber
        {
            get { return (int)GetValue(DayNumberProperty); }
            set { SetValue(DayNumberProperty, value); }
        }

        public static readonly DependencyProperty DayNumberProperty =
            DependencyProperty.Register("DayNumber", typeof(int), typeof(Code4CalendarItem), new PropertyMetadata(0,OnDayNumberChanged));

        public static void OnDayNumberChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var item = d as Code4CalendarItem;
            item.SetBackColor();
            item.SetForeColor();
        }

        public DateTime ItemDate
        {
            get { return (DateTime)GetValue(ItemDateProperty); }
            set { SetValue(ItemDateProperty, value); }
        }

        public static readonly DependencyProperty ItemDateProperty =
            DependencyProperty.Register("ItemDate", typeof(DateTime), typeof(Code4CalendarItem), new PropertyMetadata(null));


        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(Code4CalendarItem), new PropertyMetadata(false,OnIsSelectedChanged));
        public static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var item = d as Code4CalendarItem;
            item.SetBackColor();
            item.SetForeColor();
        }
        #endregion

        #region Method
        /// <summary>
        /// 设置前景色
        /// </summary>
        public void SetForeColor()
        {
            var defaultBrush = AppColorHelper.GetPhoneForeGround();
            if (_owner.ColorConverter != null && IsConverterNeeded())
            {
                Foreground = _owner.ColorConverter.Convert(ItemDate, IsSelected, IsSelected ? defaultBrush : new SolidColorBrush(Colors.Transparent), BrushType.Foreground);
            }
            else
            {
                this.Foreground = defaultBrush;
            }
        }

        /// <summary>
        /// 设置背景色
        /// </summary>
        public void SetBackColor()
        {
            var defaultbrush = new SolidColorBrush(Colors.Red);
            if (IsConverterNeeded() && _owner.ColorConverter != null)
            {
                Background = _owner.ColorConverter.Convert(ItemDate, IsSelected, IsSelected ? defaultbrush : new SolidColorBrush(Colors.Transparent), BrushType.Background);
            }
            else
            {
                Background = IsSelected ? defaultbrush : new SolidColorBrush(Colors.Transparent);
            }
        }

        /// <summary>
        /// 需要转换器
        /// </summary>
        /// <returns></returns>
        private bool IsConverterNeeded()
        {
            bool flag = true;
            if (_owner.DatesSource != null)
            {
                if (!_owner.DatesAssigned.Contains(ItemDate))
                {
                    flag = false;
                }
            }
            return flag;
        }
        #endregion

        
    }
}
